#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include "BmpShow.h"
#include "BmpFind.h"
#include "DoubleList.h"
#include "TpDetec.h"
#include "FilmFunc.h"
#include "SwitchAreaFunc.h"
#include "SlideFunc.h"

DNodep Headp = NULL;
DNodep Tempp = NULL;


int main(int argc,  char *argv[])
{
	//初始化链表头	
	int i = 0;
	int ret = 0;
	int bmpcnt = 0;
	pthread_t film_tid = 0;
	pthread_t swarea_tid = 0;
	pthread_t tp_tid = 0;
	pthread_t slide_tid = 0;

	//初始化 LCD 
	lcd_init();

	printf("hello world!\n");
	Headp = DoubleList_Create();
	
	//得到所有bmp图片
	if(argc < 2)
	{
			perror("the input is illegal");
			exit(-1);
	}
	bmpcnt = bmpfind(argv[1], Headp);
	if(Headp == NULL)
	{		
		perror("Headp is NULL!");
		exit(-1);
	}
	Tempp = Headp->next;

	//首先显示一张照片（清空原来的内容）	
	lcd_showbmp_api(Tempp->data, 0, 0, 1);
	//创建坐标扫描进程
	ret = pthread_create(&tp_tid, NULL, Tp_Detect, NULL);
	if(ret != 0)
	{
		perror("thread create failed!");
		exit(-1);
	}
#if 1
	//创建 SwitchAreaDect 进程
    lcd_showbmp_api(MENU_PIC, 730, 0,0);
	ret = pthread_create(&swarea_tid, NULL, SwitchAreaDect, NULL);
	if(ret != 0)
	{
		perror("thread create failed!");
		exit(-1);
	}
#endif


#if 1
	//创建FilmFunc进程
	ret = pthread_create(&film_tid, NULL, FilmShow_Thread, NULL);
	if(ret != 0)
	{
		perror("thread create failed!");
		exit(-1);
	}
#endif 

#if 1
	//创建Slide_Func进程
	ret = pthread_create(&slide_tid, NULL,SlideMotion_Thread, NULL);
	if(ret != 0)
	{
		perror("thread create failed!");
		exit(-1);
	}
#endif 



#if 1
	ret = pthread_join(film_tid, NULL);
	if(ret != 0)
	{
		perror("thread wait failed!");
		exit(-1);
	}
#endif

#if 1
	ret = pthread_join(swarea_tid, NULL);
	if(ret != 0)
	{
		perror("thread wait failed!");
		exit(-1);
	}
#endif 

	ret = pthread_join(tp_tid, NULL);
	if(ret != 0)
	{
		perror("thread wait failed!");
		exit(-1);
	}
	return 0;
}
